#include"lottery_sys.h"
void buy_lottery(Buyer_t* user,Lottery_t* lott_head,L_buyed_t* buyed_head)
{
	//show_lott_list(lott_head); 
	//int five = 5;
	if(user == NULL || buyed_head == NULL || lott_head == NULL)
		return;
	int i=0;
	char chose = 0;
	char buy_chose = 0;
	Lottery_t* tmp_lott =NULL;
	L_buyed_t* new_buy = NULL;
	tmp_lott = lott_head -> next;
	if(tmp_lott ==NULL)
		return;
	if(!tmp_lott -> L_yn_open)
	{
		if(user -> B_remain_money < tmp_lott -> L_unit_price)
		{
			printf("对不起，余额不足，请充值！\n");
			//sleep(2);
			return;
		}
		printf("未开奖，你还有机会购买哦! \n");
		while(1)
		{
			printf("你是否想购买？（Y/n）：");
			scanf("%c",&chose);
			cleaninput();
			if(chose == 'y' || chose == 'Y')
			{
				printf("请选择机选或输入你的号码：（1，机选 2，自选 *，放弃）：");
				scanf("%c",&buy_chose);
				cleaninput();
				new_buy = (L_buyed_t*)calloc(1,sizeof(L_buyed_t)); 
				if(buy_chose == '1')
					autochose(new_buy);										//机选彩票
				else if(buy_chose == '2')
					selfchose(new_buy);										//自选彩票
				else
					break;
				printf("\n");
				while(i < 5)
					printf("%d\t",new_buy -> chose_nums[i++]);
				printf("\n");

				user -> B_remain_money -= tmp_lott -> L_unit_price;			//用户余额处理
				strcpy(new_buy -> buyer,user -> B_name);					//用户名赋给已购彩票链表中
				new_buy -> date =  tmp_lott -> date;						//发布彩票期号赋给已购彩票链表中
				new_buy -> L_unit_price = tmp_lott -> L_unit_price;			//发布彩票单价赋给已购彩票链表中
				tmp_lott -> L_buyed_count ++;								//已买彩票的张数
				tmp_lott -> money += tmp_lott -> L_unit_price;				//彩票奖池增加
				printf("奖池金额：%f\n",tmp_lott -> money);
				if(buyed_head -> next == NULL)
					new_buy -> ID = 1;
				else
					new_buy -> ID = buyed_head -> next -> ID + 1;			//彩票ID处理
				printf("购买彩民帐号：%s\n",new_buy -> buyer);
				printf("购买彩民买的彩票ID：%ld\n",new_buy -> ID);
				//链上表
				new_buy -> next = buyed_head -> next;
				buyed_head -> next = new_buy;
			}
			else
			{
				printf("谢谢惠顾！\n");
				break;
			}
		}
	}

}

